package com.share.pulsar.rest;

import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.PulsarClientException;
import org.apache.pulsar.client.api.Schema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author caifan
 * @created 2021/5/18
 * description:
 */
@RestController
@RequestMapping("/producer")
public class PulsarProducerRest {
    @Autowired
    private PulsarClient pulsarClient;

    @PostMapping("/simple")
    public ResponseEntity producerSimpleMsg(@RequestParam String param) throws PulsarClientException  {
        Producer<String> producer = pulsarClient.newProducer(Schema.STRING)
                .topic("simple-topic")
                .producerName("simple-Producer")
                .create();

        producer.send(param);
        return ResponseEntity.ok(HttpStatus.OK);
    }
}
